<?php

namespace zhtfrontend\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%comments_info}}".
 *
 * @property integer $id
 * @property integer $valuator_type
 * @property integer $accepter_type
 * @property string $title
 * @property string $description
 * @property integer $user_id
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class CommentsInfo extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comments_info}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['valuator_type', 'accepter_type', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'valuator_type' => Yii::t('zhtfrontend', '评价者（1学生2老师3教服总监）'),
            'accepter_type' => Yii::t('zhtfrontend', '被评价者（1学生2老师3教服总监）'),
            'title' => Yii::t('zhtfrontend', '标题'),
            'description' => Yii::t('zhtfrontend', '标题描述'),
            'user_id' => Yii::t('zhtfrontend', '管理员id'),
            'status' => Yii::t('zhtfrontend', '状态（0关闭1正常）'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        $fields['user'] = function () {
            $user = Admin::findOne(['id'=>$this->user_id]);
            return \Yii::t('zhtfrontend', $user->name);
        };
        $fields['valuator_type'] = function () {
            switch ($this->valuator_type){
                case 1: return '学生';
                case 2: return '老师';
                case 3: return '教服总监';
            }
        };
        $fields['accepter_type'] = function () {
            switch ($this->accepter_type){
                case 1: return '学生';
                case 2: return '老师';
                case 3: return '教服总监';
            }
        };
        $fields['status'] = function () {
            return $this->status == 0 ? '关闭':'正常';
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
            'user' => function () {
                $user = Admin::findOne(['id'=>$this->user_id]);
                return \Yii::t('zhtfrontend', $user->name);
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'commentsinfo',
            ],
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
